<?php

class ImgickClass
{

    public $obj = null;

    public function __construct()
    {
        if (!extension_loaded('Imagick')) {
            return false;
        }
        $this->obj = new Imagick();
    }

    /*
     * 最短边固定尺寸缩放
     * @param $imagepath 图片路径
     * @param $short 缩放尺寸
     * return obj
     */
    public function resize_short($imagepath, $short)
    {
        if (!is_object($this->obj)) {
            return false;
        }
        try {
            if ($this->obj->readImage($imagepath)) {
                $imgW = $this->obj->getImageWidth();
                $imgH = $this->obj->getImageHeight();
                if ($imgW <= $imgH) {
                    $data = $this->resize_w_h($imgW, $imgH, $short, 0);
                } else {
                    $data = $this->resize_w_h($imgW, $imgH, $short, 1);
                }
                $this->obj->resizeImage($data['w'], $data['h'], Imagick::FILTER_LANCZOS, 1);
                return $this->obj->getImageBlob();
            }
        } catch (ImagickException $e) {
            return false;
        }
    }

    /*
     * 最长边固定尺寸缩放
     * @param $imagepath 图片路径
     * @param $long 缩放尺寸
     * return obj
     */
    public function resize_long($imagepath, $long)
    {
        if (!is_object($this->obj)) {
            return false;
        }
        try {
            if ($this->obj->readImage($imagepath)) {
                $imgW = $this->obj->getImageWidth();
                $imgH = $this->obj->getImageHeight();
                if ($imgW >= $imgH) {
                    $data = $this->resize_w_h($imgW, $imgH, $long, 0);
                } else {
                    $data = $this->resize_w_h($imgW, $imgH, $long, 1);
                }
                $this->obj->resizeImage($data['w'], $data['h'], Imagick::FILTER_LANCZOS, 1);
                return $this->obj->getImageBlob();
            }
        } catch (ImagickException $e) {
            return false;
        }
    }

    /*
     * 计算长宽比例
     * @param $w 原始长度
     * @param $h 原始高度
     * @param $s 固定边长
     * @param $t 固定边 0 width，1 height
     * return array $data
     */
    public function resize_w_h($w, $h, $s, $t)
    {
        $data = array();
        switch ($t) {
            case 0:
                $data['w'] = $s;
                $data['h'] = intval(($h / $w) * $s);
                return $data;
            case 1:
                $data['h'] = $s;
                $data['w'] = intval(($w / $h) * $s);
                return $data;
        }
    }

    /*
     * 精准resize
     * @param $imagepath 图片地址
     * @param $w 宽度
     * @param $h 高度
     */
    public function re_w_h($imagepath, $w, $h)
    {
        if (!is_object($this->obj)) {
            return false;
        }
        try {
            if ($this->obj->readImage($imagepath)) {
                $this->obj->resizeImage($w, $h, Imagick::FILTER_LANCZOS, 1);
                return $this->obj->getImageBlob();
            }
        } catch (ImagickException $e) {
            return false;
        }
    }

    function destory()
    {
        if (is_object($this->obj)) {
            $this->obj->clear();

            $this->obj->destroy();
        }
    }

}